<template>
  <div class="comment">
    <myheader title="精彩评论">
      <template #left>
        <van-icon name="arrow-left" @click="$router.go(-1)" />
      </template>
    </myheader>
    <div class="list" style="padding-bottom: 60px">
      <div class="item" v-for="item in commentList" :key="item.id">
        <div class="head">
          <img :src="axios.defaults.baseURL + item.user.head_img" alt />
          <div>
            <p>{{ item.user.nickname }}</p>
            <span>时间</span>
          </div>
          <span @click="send(item)">回复</span>
        </div>
        <!-- 不是每个评论对象都是parent属性的，如果没有parent属性，则说明没有上一级评论数据，则不用再生成这种评论结构了 -->
        <mycommentItem
          v-if="item.parent"
          :commentParent="item.parent"
          @sendComment='send'  
        ></mycommentItem>
        <div class="text">{{ item.content }}</div>
      </div>
    </div>
    <mycommentFooter
      :article="post"
      @refresh="refresh"
      :commentObj="temp"
       @reset='temp=null'
    ></mycommentFooter>
  </div>
</template>

<script>
import myheader from "@/components/myheader";
import mycommentItem from "@/components/myCommentItem";
import mycommentFooter from "@/components/mycommentFooter";
import { getPostById, getPostCommentById } from "@/apis/post";
import axios from "@/utils/request";
export default {
  data() {
    return {
      //   axios:axios,
      axios,
      post: {},
      commentList: [],
      temp: {},
    };
  },
  components: {
    myheader,
    mycommentFooter,
    mycommentItem,
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      // 根据文章id获取文章详情数据
      getPostById(this.$route.params.id).then((res) => {
        console.log(res);
        this.post = res.data.data;
      });
      //根据文章id获取当前文章的评论数据
      getPostCommentById(this.$route.params.id, { pageSize: 100 }).then(
        (res) => {
          console.log(res);
          this.commentList = res.data.data;
        }
      );
    },
    //  1.通过父传子的方式，将当前回复的评论对象传递给子组件
    send(value) {
        console.log(value);
      this.temp = value;
    },
    // 解决评价完回到页面顶部
    refresh() {
      this.init();
      window.scrollTo(0, 0);
    },
  },
};
</script>

<style lang="less" scoped>
.comment {
  .item {
    padding: 10px;
    border-bottom: 1px solid #ccc;
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      > img {
        width: 50/360 * 100vw;
        height: 50/360 * 100vw;
        display: block;
        border-radius: 50%;
      }
      > div {
        flex: 1;
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        > span {
          font-size: 12px;
          color: #999;
          line-height: 25px;
        }
      }
      > span {
        color: #999;
        font-size: 13px;
      }
    }
    .text {
      font-size: 14px;
      color: #333;
      padding: 20px 0 10px 0;
    }
  }
}
</style>